package com.atguigu.blog.service.impl;

import com.atguigu.blog.dao.LogInfoDao;
import com.atguigu.blog.pojo.LogInfo;
import com.atguigu.blog.service.LogInfoService;
import com.atguigu.blog.util.IdWorker;
import com.atguigu.blog.util.UserInfoUtil;
import com.atguigu.blog.util.WebUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoDao, LogInfo> implements LogInfoService {

    @Autowired
    private IdWorker idWorker;

    @Override
    public void saveLogInfo(HttpServletRequest request, String logName) {
        String ip = UserInfoUtil.getIp(request);
        String host = UserInfoUtil.getHost(request);
        String user = UserInfoUtil.getUser(request);
        Integer port = UserInfoUtil.getPort(request);
        String header = request.getHeader("user-agent");
        // 通过IP获取城市信息
        String cityName = WebUtil.getCityByIP(ip);
        String browserName = WebUtil.getBrowserName(header);
        LogInfo logInfo = new LogInfo();
        logInfo.setLogAddr(host);
        logInfo.setCreateDate(new Date());
        logInfo.setUpdateDate(new Date());
        logInfo.setLogAddrIp(ip);
        logInfo.setCityName(cityName);
        logInfo.setBrowserName(browserName);
        logInfo.setLogName(logName);
        baseMapper.insert(logInfo);
    }
}
